<?php
namespace Admin\Controller;
use Think\Controller;
class LoginController extends Controller {
    public function index()
    {
        $this->display();
    }

    //login
    public function login()
    {
    	if (IS_POST) {
    		$username = I('post.username');
    		$password = md5(I('post.password'));

    		$map['username'] = $username;
    		$map['password'] = $password;
    		$res = M('gy_admin')->where($map)->find();
    		if ($res) {
    			$url = U('admin/index/index');
    			$arr = array(
                    'error' => 1 ,
                    'info' => '登陆成功',
                    'url' => $url
                    );
                echo json_encode($arr);

                //更新登陆时间/ip 写入session 
                $login_time= time();
                $ip = get_client_ip();
                $data = array('login_time'=>$login_time,'ip'=>$ip);
                M('gy_admin')->where(array('id'=>$res["id"]))->setField($data);

                //设置session登录成功
                $_SESSION["id"] = $res["id"];
                $_SESSION["username"] = $res["username"];
    		}else {
                $arr = array(
                    'error' => 0 ,
                    'info' => '账号密码错误'
                    );
                echo json_encode($arr);
    		}
    	}
    }

    //退出
    public function logout()
    {
        session_unset();
        session_destroy();
        $this->redirect('admin/login/index');
    }




}